package roborally.model;
import roborally.gui.*;

public class Facade implements IFacade{

	@Override
	public IRobot createRobot(long x, long y, int orientation, double energy) {
		if(Robot.isValidEnergy(energy,20000)) //assure that the preconditions are true, as we are the caller ("the user") of the method.
			return (IRobot)(new Robot(x,y,Orientation.values()[orientation],energy));
		else
			return null;
	}

	@Override
	public long getX(IRobot robot){
		return ((Robot)robot).getPosition().getX();	
	}

	@Override
	public long getY(IRobot robot) {
		return ((Robot)robot).getPosition().getY();
	}

	@Override
	public int getOrientation(IRobot robot) {
		return ((Robot)robot).getIntOrientation();
	}

	@Override
	public void move(IRobot robot) {
		if(Robot.isValidEnergy( ((Robot)robot).getEnergy()-Robot.ECOST_MOVE, ((Robot)robot).getMaxEnergy()) ) //assure that the precondition is true
		{
			try{
				((Robot)robot).move();
			}
			catch (IllegalArgumentException exc)
			{
				//don't move if moving results in an exception being thrown
				System.err.print("An exception was thrown! The robot moved out of bounds.\n");
			}
		}
	}

	@Override
	/**
	 * @pre		...
	 * 			|isValidEnergy(((Robot)robot).getEnergy()-100,((Robot)robot).getMaxEnergy())
	 * @effect 	...
	 * 			|((Robot)robot).Turn(true);
	 */
	public void turnClockwise(IRobot robot) {
		if( Robot.isValidEnergy( ((Robot)robot).getEnergy() - Robot.ECOST_TURN, ((Robot)robot).getMaxEnergy() ) ) //assure that the precondition is true
				((Robot)robot).turn(Robot.CLOCKWISE);
	}

	@Override
	public double getEnergy(IRobot robot) {
		return ((Robot)robot).getEnergy();
	}

	@Override
	public void recharge(IRobot robot, double energyAmount) {
		if( energyAmount >= 0 && Robot.isValidEnergy( ((Robot)robot).getEnergy() + energyAmount, ((Robot)robot).getMaxEnergy() ) ) //assure that the precondition is true
			((Robot)robot).recharge(energyAmount);
	}

	@Override
	public int isGetEnergyRequiredToReachAndMoveNextTo16Plus() {
		return 1;
	}

	@Override
	public double getEnergyRequiredToReach(IRobot robot, long x, long y) {
		return ((Robot)robot).getEnergyRequiredToReach(x, y);
	}

	@Override
	public void moveNextTo(IRobot robot, IRobot robot2) {
		Robot.moveNextTo((Robot)robot,(Robot)robot2);
	}

}
